﻿#region Copyright

//  ======================================================
//      Copyright (c) 2011 Toulr All rights reserved.
//     
//      The use and distribution terms for this software are contained in the file
//      named license.txt, which can be found in the root of this distribution.
//      By using this software in any fashion, you are agreeing to be bound by the
//      terms of this license.
//     
//     You must not remove this notice, or any other, from this software.
//  ======================================================

#endregion

#region Using Namespaces

using System;
using System.Diagnostics;
using System.IO;
using NUnit.Framework;
using Toulr.Domains;
using Toulr.Core.Services;
using Toulr.Domains.Business;
using Toulr.Models;
using Toulr.Services;
using Toulr.Util.Ioc;
using Toulr.Util.Security;

#endregion

namespace Toulr.Test.Services
{
    [TestFixture]
    public class ApplicationContextServiceFixture
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
            //从容器内初始化接口的实例
            _applicationContextService = UnityHelper.GetInstance<IApplicationContextService>();
        }

        #endregion

        private IApplicationContextService _applicationContextService;

        /// <summary>
        ///   测试获取省份信息
        /// </summary>
        [Test]
        public void GetProvincesTest()
        {
            var result = _applicationContextService.GetProvinces();
            Assert.NotNull(result);
        }

        /// <summary>
        ///   测试图片校验码生成代码是否正常工作
        /// </summary>
        [Test]
        public void VerifyCodeTest()
        {
            ActionResult<VerifyImage> verifyImage = _applicationContextService.GetVerifyCode();
            const string file = "test_verify.bmp";
            if (File.Exists(file))
                File.Delete(file);
            verifyImage.Result.Image.Save(file);
            Assert.NotNull(verifyImage);
        }

        /// <summary>
        /// 测试获取快递公司列表
        /// </summary>
        [Test]
        public void GetExpressDeliveryTest()
        {
            var result = _applicationContextService.GetExpressDeliverys();
            Assert.NotNull(result);

            var result1 = _applicationContextService.GetExpressDeliverys(-1, "通", true);
            Assert.NotNull(result1);

            var result2 = _applicationContextService.GetExpressDeliverys(1, null, null);
            Assert.NotNull(result2);

            var result3 = _applicationContextService.GetExpressDeliverys(-1, null, false);
            Assert.NotNull(result3);

            var result4 = _applicationContextService.GetExpressDeliverys(2, "通",true);

            Assert.NotNull(result4);
        }

        /// <summary>
        /// 测试添加快递公司信息
        /// </summary>
        [Test]
        public void AddExpressDeliveryTest()
        {
            var v1 = new ExpressDelivery
                         {
                             Description = "新增加的1个快递公司",
                             Enabled = true,
                             ExpressDeliveryService = "http://www.toulr.com",
                             Name = "tol快递",
                             Id = -1,
                             Prices = 5,
                             PricesPerKg = 1
                         };
            var result1=_applicationContextService.AddExpressDelivery(v1);
            Assert.NotNull(result1.Result);
        }

        /// <summary>
        /// 测试修改快递公司信息
        /// </summary>
        [Test]
        public void UpdateExpressDeliveryTest()
        {
            //只有ID和描述
            var expressDelivery = new ExpressDelivery {Description = "修改1",Id=4};
            _applicationContextService.UpdateExpressDelivery(expressDelivery);

            //全部信息
            expressDelivery = new ExpressDelivery
                                  {
                                      Description = "修改1",
                                      Enabled = true,
                                      ExpressDeliveryService = "http://www.toulr.com",
                                      Id = 4,
                                      Name = "toulr快递",
                                      Prices = 10,
                                      PricesPerKg = 0.5f
                                  };
            _applicationContextService.UpdateExpressDelivery(expressDelivery);
            
            //没有ID
            expressDelivery = new ExpressDelivery
            {
                Description = "修改1",
                Enabled = true,
                ExpressDeliveryService = "http://www.toulr.com",
                Name = "toulr快递",
                Prices = 10,
                PricesPerKg = 0.5f
            };
            _applicationContextService.UpdateExpressDelivery(expressDelivery);

            //不存在的ID
            expressDelivery = new ExpressDelivery { Id = 999 };
            _applicationContextService.UpdateExpressDelivery(expressDelivery);
        }

        /// <summary>
        /// 测试删除快递公司信息
        /// </summary>
        [Test]
        public void DeleteExpressDeliveryTest()
        {
            //正确删除
            var expressDelivery = new ExpressDelivery {  Id = 4 };
            _applicationContextService.DeleteExpressDelivery(expressDelivery);
            //删除不存在的ID
            expressDelivery = new ExpressDelivery { Id = 4 };
            _applicationContextService.DeleteExpressDelivery(expressDelivery);
        }
    }
}